<?php
/** [图集表逻辑层模型]
 * @Author: 976123967@qq.com
 * @Date:   2015-01-17 14:56:43
 * @Last Modified by:   chenli
 * @Last Modified time: 2015-03-05 23:50:54
 */
class PicLogic extends Model{
	public $tableName = 'article_pic';
	public $model;
	public $small;
	public $medium;

	public function _initialize()
	{
		$this->model = D('ArticlePic');

		// 图集尺寸
		$this->small['width']   = C('cfg_pic_small_width');
		$this->small['height']  = C('cfg_pic_small_height');
		$this->medium['width']  = C('cfg_pic_medium_width');
		$this->medium['height'] = C('cfg_pic_medium_height');
	}

	/**
	 * [add_pic 添加图集]
	 * @param [type] $aid [description]
	 */
	public function add_pic($aid)
	{
		if(!empty($_FILES['img']['name']))
        {
            import('ORG.Net.UploadFile');
            $upload = new UploadFile();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小

            $ext = explode('|', C('cfg_image'));
            $upload->allowExts  = $ext;// 设置附件上传类型

            $savePath ='./Data/Uploads/image/'.date('Y').'/'.date('m').'/'.date('d').'/';

            //$picTitle = $_POST['pic_title'];
        
            
            is_dir($savePath) || mkdir($savePath,0777,true);
            if($info=$upload->uploadOne($_FILES['img'],$savePath)) 
            {   // 上传成功 获取上传文件信息
               

                // 获取比例
                $small_width = $this->small['width'];
                $small_height = $this->small['height'];
                $medium_width = $this->medium['width'];
                $medium_height = $this->medium['width'];

           

                foreach($info as $k=> $f)
                {
                    // 定义路径
                    $f['path']=$f['savepath'].$f['savename'];
                    $pic=pathinfo($f['path']);
                    // 缩略图
                    
                    $medium=$pic['dirname'] . '/' .$pic['filename']."_medium.".$pic['extension'];
                    $small = $pic['dirname'] . '/' .$pic['filename']."_small.".$pic['extension'];
                    import("ORG.Util.Image");
                    Image::thumb2($f['path'],$medium,$pic["extension"],$medium_width,$medium_height);
                    Image::thumb2($f['path'],$small,$pic["extension"],$small_width,$small_height);

                    // 添加
                    $this->add(array(
                        'article_aid'=> $aid,
                        'big'=>$f['path'],
                        'medium'=>$medium,
                        'small'=>$small,
                        //'pic_title'=>$picTitle[$k]
                    ));  
                }
            }

        }
	}


	/**
	 * [get_all 获取图片]
	 * @return [type]      [description]
	 */
	public function get_all()
	{
		$aid  = I('get.aid');
 		$data = $this->where(array('article_aid'=>$aid))->select();
 		if(!$data) return false;

 		// 组合路径
 		foreach ($data as $k=>$v)
 		{
 			$data[$k]['small'] = __ROOT__.'/'.$v['small'];
 			$data[$k]['medium'] = __ROOT__.'/'.$v['medium'];
 			$data[$k]['big'] = __ROOT__.'/'.$v['big'];
 		}
 		return $data;
	}

	/**
	 * [del_pic 删除图片]
	 * @return [type] [description]
	 */
	public function del_pic()
	{
		$id  = I('get.id');
		$aid = I('get.aid');
		$data = $this->where(array('article_aid'=>$aid,'id'=>$id))->find();
		if(!$data)
		{
			$this->error = '链接错误';
			return false;
		}

		// 删除图片
		is_file($data['small'])  && unlink($data['small']);
		is_file($data['medium']) && unlink($data['medium']);
		is_file($data['big'])    && unlink($data['big']);

		$this->delete($id);


		return true;

	}

	/**
	 * [delete_pic_by_article_aid 删除图片]
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function delete_pic_by_article_aid($aid)
	{
		$data = $this->where(array('article_aid'=>$aid))->select();
		if($data)
		{
			foreach($data as $v)
			{
				// 删除图片
				is_file($v['small'])  && unlink($v['small']);
				is_file($v['medium']) && unlink($v['medium']);
				is_file($v['big'])    && unlink($v['big']);

			}
		}
	}

	
}